package com.llw.testdemo.adapter;

import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.llw.testdemo.R;
import com.llw.testdemo.bean.TestResponse;
import com.zzhoujay.richtext.ImageHolder;
import com.zzhoujay.richtext.RichText;
import com.zzhoujay.richtext.RichType;

import java.util.ArrayList;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

public class TestAdapter extends BaseQuickAdapter<TestResponse.DataBean, BaseViewHolder> {
    public TestAdapter(int layoutResId, @Nullable List<TestResponse.DataBean> data) {
        super(layoutResId, data);
    }

    private List<String> mList;

    private ImgAdapter mAdapter;
    CircleImageView headImg;

    @Override
    protected void convert(BaseViewHolder helper, TestResponse.DataBean item) {
        headImg = helper.getView(R.id.head_image);
        ImageView sexImg = helper.getView(R.id.iv_sex);
        TextView tvContent = helper.getView(R.id.tv_content);
        //头像显示
        Glide.with(mContext)
                .load(item.getUserInfo().getHeadImage())
                .placeholder(R.drawable.ic_launcher_background)
                .into(headImg);
        switch (item.getUserInfo().getSex()) {
            case "0"://女
                sexImg.setBackgroundResource(R.mipmap.icon_woman);
                break;
            case "1"://男
                sexImg.setBackgroundResource(R.mipmap.icon_man);
                break;
        }
        helper.setText(R.id.tv_nickname, item.getUserInfo().getUsername())//用户昵称
                .setText(R.id.tv_title, item.getTitle());//标题
        RichText.from(item.getContent()).bind(mContext)
                .type(RichType.html)
                .showBorder(false)//无边框
                .size(ImageHolder.MATCH_PARENT, ImageHolder.WRAP_CONTENT)//图片尺寸
                .into(tvContent);

        //图片列表
        RecyclerView rvImg = helper.getView(R.id.rv_img);

        mList = new ArrayList<>();
        mList = item.getImages();
        mAdapter = new ImgAdapter(R.layout.item_img_list, mList);
        GridLayoutManager manager;
        if (item.getImages().size() >= 3) {
            manager = new GridLayoutManager(mContext, 3);

        } else {
            manager = new GridLayoutManager(mContext, 2);
        }
        rvImg.setLayoutManager(manager);
        rvImg.setAdapter(mAdapter);
        mAdapter.notifyDataSetChanged();
    }


}
